﻿#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
#endregion

namespace XmlGenSharp.Logic.Main
{
  public class SingleOutputAnalyzerContext : AnalyzerContext
  {
    // Constructors

    #region SingleOutputAnalyzerContext(BindingAnalyzer bindingAnalyzer)
    public SingleOutputAnalyzerContext(BindingAnalyzer bindingAnalyzer) : base(bindingAnalyzer)
    {
    }
    #endregion

    // Public Properties

    #region CompoundGenerator
    public CompoundGenerator CompoundGenerator
    {
      get
      {
        if (_compoundGenerator == null)
          _compoundGenerator = new CompoundGenerator(BindingAnalyzer.InputFileName, BindingAnalyzer.DefaultNamespace);

        return _compoundGenerator;
      }
    }
    private CompoundGenerator _compoundGenerator = null;
    #endregion

    // Public Methods

    #region FoundComplexType(XmlSchemaComplexType complexType)
    public override void FoundComplexType(XmlSchemaComplexType complexType)
    {
      CompoundGenerator.Add(new ClassGenerator(BindingAnalyzer.InputFileName, BindingAnalyzer.DefaultNamespace, complexType));
    }
    #endregion

    #region FoundSimpleType(XmlSchemaSimpleType simpleType)
    public override void FoundSimpleType(XmlSchemaSimpleType simpleType)
    {
      CompoundGenerator.Add(new EnumGenerator(BindingAnalyzer.InputFileName, BindingAnalyzer.DefaultNamespace, simpleType));
    }
    #endregion

    #region FoundEnumeration(EnumerationInfo enumerationInfo)
    public override void FoundEnumeration(EnumerationInfo enumerationInfo)
    {
      AdvancedEnumGenerator aeg = new AdvancedEnumGenerator(BindingAnalyzer.InputFileName, BindingAnalyzer.DefaultNamespace, enumerationInfo);

      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      AppendCommonPreText(sw, BindingAnalyzer.DefaultNamespace);

      aeg.PreText = sb.ToString();
      aeg.PostText = "}";

      CompoundGenerator.Add(aeg);
    }
    #endregion

    #region OnBeforeAnalyzeFromJobFile()
    public override void OnBeforeAnalyzeFromJobFile()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      AppendJobInfo(sw);
      AppendCommonPreText(sw, CompoundGenerator.Namespace);

      CompoundGenerator.PreText = sb.ToString();
      CompoundGenerator.PostText = "}";
    }
    #endregion

    #region OnBeforeAnalyzeFromXsd()
    public override void OnBeforeAnalyzeFromXsd()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      AppendXsdInfo(sw);
      AppendCommonPreText(sw, CompoundGenerator.Namespace);

      CompoundGenerator.PreText = sb.ToString();
      CompoundGenerator.PostText = "}";
    }
    #endregion
  }
}
